package com.maikang.exammall.interceptor;

import com.maikang.exammall.entity.User;
import com.maikang.exammall.entity.UserContext;
import com.maikang.exammall.exception.MobileNotBoundException;
import com.maikang.exammall.exception.UnauthorizedException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Component
public class MobileRequiredInterceptor implements HandlerInterceptor {

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		User user = UserContext.getUser();
		if (user == null || user.getId() == null) {
			throw new UnauthorizedException();
		}
		if (!StringUtils.hasLength(user.getMobile())) {
			throw new MobileNotBoundException();

		}
		return true;
	}

}
